﻿/*----------------------------------------------------------------
// Copyright (C) 2011 周士祎
// 版权所有。 
//
// 文件名：DataAccessRoles
// 文件功能描述：角色管理数据层
//
// 
// 创建标识：2012-01-03 周士祎
//
// 修改标识：
// 修改描述：
//
//
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using DbAdapter.sqldbAdapter;

namespace DAO.WaiGaoQiao
{
    public class DataAccessRoles
    {
        /// <summary>
        /// 获取所有角色
        /// </summary>
        /// <returns></returns>
        public DataSet GetAllRoles(int pagesize, int startindex)
        {
            using(SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                int startrow = pagesize * (startindex - 1);
                int endrow = pagesize * startindex + 1;
                string query = "select B.RoleId,B.RoleName,B.RoleMeno from (select ROW_NUMBER() OVER(ORDER BY A.RoleId DESC) rownumber,"
                             + "A.RoleId,A.RoleName,A.RoleMeno from Tbt_Roles A) B "
                             + "where B.rownumber > " + startrow.ToString() + " and B.rownumber < " + endrow.ToString();
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                return ds;
            }
        }
        public DataSet GetAllRoles()
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                string query = "select A.RoleId,A.RoleName,A.RoleMeno from Tbt_Roles A";
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                return ds;
            }
        }
        /// <summary>
        /// 角色数量
        /// </summary>
        /// <returns></returns>
        public int GetRolesCount()
        {
            sqldb db = new sqldb();
            string query = "select count(1) Tbt_Roles";
            object rowcount = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return Convert.ToInt32(rowcount);
        }
        /// <summary>
        /// 保存角色
        /// </summary>
        /// <param name="rolepara"></param>
        /// <returns></returns>
        public int SaveRole(object[] rolepara)
        {
            sqldb db = new sqldb();
            string query = "Insert Into Tbt_Roles(RoleId,RoleName,RoleMeno) Values('" + rolepara[0].ToString() + "','"
                         + rolepara[1].ToString() + "','" + rolepara[2].ToString() + "')";
            int rowcount = db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
            return rowcount;
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public int DeleteRole(string roleid)
        {
            sqldb db = new sqldb();
            string query = "Delete from Tbt_Roles where RoleId = '" + roleid + "'";
            int rowcount = db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
            return rowcount;
        }

        /// <summary>
        /// 角色绑定到登录用户
        /// </summary>
        /// <param name="roleid"></param>
        /// <param name="logid"></param>
        /// <returns></returns>
        public int RoleBindLog(string roleid, string logid)
        {
            sqldb db = new sqldb();
            string query = "Insert Into Tbt_LogUserAndRole(LogName,RoleId) Values('" + logid + "','" + roleid + "')";
            int rowcount = db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
            return rowcount;
        }

        /// <summary>
        /// 删除绑定
        /// </summary>
        /// <param name="roleid"></param>
        /// <param name="logid"></param>
        /// <returns></returns>
        public int NotRoleBindLog(string roleid, string logid)
        {
            sqldb db = new sqldb();
            string query = "Delete from Tbt_LogUserAndRole where LogName = '" + logid + "' and RoleId = '" + roleid + "'";
            int rowcount = db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
            return rowcount;
        }
    }
}
